package com.mymoney;

import java.util.Date;

import utils.DateTimeUtil;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class TransactionDetailFrag extends Fragment {
	
	static final int FONT_SIZE = 18;
	
	private Date reportDate = new Date();
	
	public TransactionDetailFrag(Date date) {
		reportDate = date;
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
		// Load data of this date
		return inflater.inflate(R.layout.transaction_detail, container, false);
    }
	@Override
	public void onStart() {
		super.onStart();
		this.loadData();
		TextView month = (TextView)getView().findViewById(R.id.month);
		TextView date = (TextView)getView().findViewById(R.id.date);
		
		month.setText(DateTimeUtil.getMonthName(reportDate.getMonth()));
		date.setText(String.valueOf(reportDate.getDate()));
	};
	
	public void loadData()
	{
		this.createRow("Food", 10000, false);
		this.createRow("Food", 10000, false);
		this.createRow("Coffee", 90000, false);
		this.createRow("Food", 10000, false);
		this.createRow("Coffee", 90000, true);
	}
	
	public void createRow(String title, float price, boolean outcome)
    {	
		TableLayout tl = (TableLayout) getView().findViewById(R.id.list_trans);
		View v = tl.getChildAt(tl.getChildCount()-1);
		tl.removeView(v);
		
        /* Create a new row to be added. */
        TableRow tr = new TableRow(getActivity());
        tr.setLayoutParams(new LayoutParams(
                       LayoutParams.FILL_PARENT,
                       LayoutParams.WRAP_CONTENT));
             
    	// first collum
    	TextView titleView = new TextView(getActivity());
    	titleView.setTextSize(FONT_SIZE);
    	titleView.setTextColor(Color.BLACK);
    	titleView.setText(title);
    	titleView.setGravity(Gravity.LEFT);
        tr.addView(titleView);
         
        // seconds collum
        TextView priceView = new TextView(getActivity());
        priceView.setTextSize(FONT_SIZE);
        
        String strAdd = "";
        if (outcome)
        {
        	strAdd = "-";
        	priceView.setTextColor(Color.RED);
        }
        else 
        {
        	strAdd = "+";
        	priceView.setTextColor(Color.BLUE);
        }
        priceView.setGravity(Gravity.RIGHT);
        String combineString = strAdd + Float.toString(price);
        priceView.setText(combineString);
     	tr.addView(priceView);
     	
     	if (tl.getChildCount() % 2 == 0)
     	{
     		tr.setBackgroundColor(Color.YELLOW);
     	}else
     	{
     		tr.setBackgroundColor(Color.GRAY);
     	}
     	tl.addView(tr,new TableLayout.LayoutParams(
	             LayoutParams.FILL_PARENT,
	             LayoutParams.WRAP_CONTENT));
     	
     	tl.addView(v);
    }

}
